﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 慢截图
{
    class ScreenShotor
    {
        private string savePath;
        public string dateTimeFormat;

        public ScreenShotor(string savePath, string dateTimeFormat = "yy-MM-dd-hh-mm-ss")
        {
            this.savePath = savePath;
            this.dateTimeFormat = dateTimeFormat;
        }

        private Bitmap GetScreenCapture()
        {
            Rectangle tScreenRect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Bitmap tSrcBmp = new Bitmap(tScreenRect.Width, tScreenRect.Height); // 用于屏幕原始图片保存
            Graphics gp = Graphics.FromImage(tSrcBmp);
            gp.CopyFromScreen(0, 0, 0, 0, tScreenRect.Size);
            gp.DrawImage(tSrcBmp, 0, 0, tScreenRect, GraphicsUnit.Pixel);
            return tSrcBmp;
        }

        private void Save(Bitmap bitmap) 
        {
            string timeString =  DateTime.Now.ToString(dateTimeFormat)+ ".jpg";
            string fileName = Path.Combine(savePath, timeString);
            using (var fs = File.OpenWrite(fileName))
            {
                bitmap.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }

        public void ScreenShotOne() 
        {
            Bitmap bitmap= GetScreenCapture();
            Save(bitmap);
        }
    }
}
